<template>
	<view class="sun-index">
		<view class="sun-logo-box"></view>
		<view class="sun-login-box">
			<view class="sun-label">
				<image style="width: 28rpx;height:39rpx;" src="@/static/imgs/mobile_icon.png" />
				<text class="label-text"></text>
			</view>
			<view class="sun-input-box">
				<input v-model="state.user.userEmail" type="text" placeholder="邮箱"
					placeholder-class="placeholder-class" />
				<image @click="state.user.userEmail=''" class="close-icon" src="@/static/imgs/close_icon.png" />
			</view>
		</view>
		<view class="sun-login-box">
			<view class="sun-label">
				<image style="width: 29rpx;height:37rpx;" src="@/static/imgs/pwd_icon.png" />
				<text class="label-text"></text>
			</view>
			<view class="sun-input-box">
				<input v-model="state.user.userPassword" type="password" placeholder="密码"
					placeholder-class="placeholder-class" />
				<image @click="state.user.userPassword=''" class="close-icon" src="@/static/imgs/close_icon.png" />
			</view>
		</view>
		<view class="sun-tip">
			<text class="sun-tip-text"></text>
			<text class="sun-tip-text" @click="goRegister">注册</text>
		</view>
		<view class="login-btn-box">
			<view class="login-btn" @click="handleSubmit">登录</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onMounted,
		onActivated,
		reactive
	} from 'vue'
	import {
		useAppStore
	} from '@/store/modules/app.js'
	const state = reactive({
		isWentTo: '',
		user: {
			userEmail: '',
			userPassword: ''
		}
	})
	const storeUser = useAppStore()

	function goRegister() {
		uni.navigateTo({
			url: '/pages/register/register'
		})
	}
	
	onActivated(() => {
		if (storeUser.userId === '') {
			uni.showToast({
				title: "未登录",
				icon: 'none',
				duration: 1500
			})
			return;
		}
	})
	
	onMounted(() => {
		if (storeUser.userId === '') {
			uni.showToast({
				title: "未登录",
				icon: 'none',
				duration: 1500
			})
			return;
		}
	})

	function handleSubmit() {
		uni.request({
			url: 'http://175.178.249.171:8092/api/user/login',
			method: 'POST',
			data: JSON.stringify(state.user),
			success: (res) => {
				if (res.data.data === "不存在此用户") {
					uni.showToast({
						title: "账号密码错误",
						icon: 'none',
						duration: 1500
					})
				} else {
					storeUser.userEmail=state.user.userEmail
					storeUser.userId=res.data.data
					uni.showToast({
						title: "登录成功",
						icon: 'none',
						duration: 1500
					})
					uni.switchTab({
						url: '/pages/user/user'
					})
				}
			},
			fail: (res) => {}
		})

	}
</script>

<style scoped>
	.sun-logo-box {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 750rpx;
		height: 300rpx;
	}

	.sun-logo {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 180rpx;
		height: 180rpx;
		border-radius: 15rpx;
		background-color: #12C8B9;
		box-shadow: 0rpx 0rpx 30rpx rgba(18, 200, 185, 0.5);
	}

	.close-icon {
		width: 36rpx;
		height: 34rpx;
	}

	.sun-icon-img {
		width: 120rpx;
		height: 120rpx;
	}

	.sun-login-box {
		padding: 20rpx 60rpx;
	}

	.sun-label {
		display: flex;
		align-items: center;
	}

	.label-text {
		margin-left: 16rpx;
		font-weight: 500;
		color: #272e2d;
		font-size: 30rpx;
	}

	.sun-input-box {
		display: flex;
		align-items: center;
		height: 100rpx;
		border-bottom: 1rpx solid #eee;
		padding: 0px 10rpx;
	}

	.sun-input-box input {
		flex: 1;
	}

	.placeholder-class {
		font-size: 30rpx;
		color: #C0C0C0;
	}

	.sun-tip {
		display: flex;
		justify-content: space-between;
		padding: 0rpx 68rpx;
	}

	.sun-tip-text {
		color: #30C6B3;
	}

	.login-btn-box {
		position: absolute;
		bottom: 129rpx;
		left: 0;
		width: 750rpx;
		padding: 50rpx 68rpx 0rpx;
	}

	.login-btn {
		height: 82rpx;
		border-radius: 41rpx;
		background-color: #12C8B9;
		box-shadow: -1px 12px 11px 0px rgba(16, 170, 157, 0.4);
		text-align: center;
		line-height: 82rpx;
		font-size: 36rpx;
		font-weight: 500;
		color: #fff;
	}
</style>
